#!/bin/bash
#设置任务异常退出
set -eo pipefail
dt='{{ execution_date | cst_ds }}'
name='温台代理区t-1揽收数据'
# 在退出时清理文件
trap "rm -f /tmp/${name}_${dt}.zip /tmp/${name}_${dt}.csv && echo 'removed tmp files'" EXIT
# 创建txt文件
touch /tmp/${name}_${dt}.txt
#将中文字段名输入文件顶部
#将查询结果追加到文件中
{
  echo -e "运单号\t寄件网点\t寄件代理区\t寄件市\t寄件区县\t揽收时间\t入仓时间\t始发转运中心\t始发转运中心到车时间\t支线任务单号\t干线任务单号\t始发转运到件时间\t始发转运发件时间\t干线任务号（中心-中心）\t目的转运中心\t签收网点\t签收代理区\t签收市\t签收区县\t签收时间">/tmp/${name}_${dt}.txt;
  spark-sql --driver-memory 8G --num-executors 10 --executor-memory 12g --executor-cores 4  --conf spark.executor.memoryOverhead=4g --conf spark.driver.maxResultSize=3g -S -e "
          select nvl(billcode, ' ')                                             as billcode
               , nvl(send_inputsite, ' ')                                       as send_inputsite
               , nvl(send_subordinate_agent_name, ' ')                          as send_subordinate_agent_name
               , nvl(send_city_desc, ' ')                                       as send_city_desc
               , nvl(send_area_desc, ' ')                                       as send_area_desc
               , nvl(date_format(collect_scantime, 'yyyy-MM-dd HH:mm:ss'), ' ') as collect_scantime
               , nvl(date_format(input_scantime, 'yyyy-MM-dd HH:mm:ss'), ' ')   as input_scantime
               , nvl(center_first_inputsite, ' ')                               as center_first_inputsite
               , nvl(date_format(center_first_clock_time, 'yyyy-MM-dd HH:mm:ss'),' ')    as center_first_clock_time
               , nvl(branch_shipment_no, ' ')                                   as branch_shipment_no
               , nvl(gan_shipment_no, ' ')                                      as gan_shipment_no
               , nvl(date_format(center_first_arrival_time, 'yyyy-MM-dd HH:mm:ss'),' ')  as center_first_arrival_time
               , nvl(date_format(center_first_send_time, 'yyyy-MM-dd HH:mm:ss'),' ' )     as center_first_send_time
               , nvl(gan_shipment_no_center, ' ')                               as gan_shipment_no_center
               , nvl(center_name, ' ')                                          as center_name
               , nvl(signsite, ' ')                                             as signsite
               , nvl(sign_subordinate_agent_name, ' ')                          as sign_subordinate_agent_name
               , nvl(sign_city_desc, ' ')                                       as sign_city_desc
               , nvl(sign_area_desc, ' ')                                       as sign_area_desc
               , nvl(date_format(sign_time, 'yyyy-MM-dd HH:mm:ss'),' ')                  as sign_time
          from jms_dm.dm_cn_export_pro_aging_data_dt
          where dt = '{{ execution_date | cst_ds }}'
            and send_subordinate_agent_name = '温台代理区'
          order by scantime
;
 " >>/tmp/${name}_${dt}.txt
}

# 替换分隔符，并输出csv
cat /tmp/${name}_${dt}.txt | tr -s '\t' ',' > /tmp/${name}_${dt}.csv
# 压缩并删除 csv 文件
zip -jrm /tmp/${name}_${dt}.zip /tmp/${name}_${dt}.csv
# 上传文件
/usr/local/python3/bin/python3  /usr/local/airflow/dags/utils/operators/upload_oss.py ${name}/${name}_${dt}.zip /tmp/${name}_${dt}.zip
# zip 文件将在进程退出后被自动清理

#声明文件大小
data_size=$(ls -lh /tmp/${name}_${dt}.zip | awk '{print $5}')
#打印文件信息
Message="${name}_${dt}.zip 上传成功 大小: $data_size"
#配置钉钉告警
curl 'https://oapi.dingtalk.com/robot/send?access_token=816a8f35fd84432ffa6bff9d7714abc322628bb4a8ded86908550d2335aba613' \
   -H 'Content-Type: application/json' \
   -d "
  {\"msgtype\": \"text\",
    \"text\": {
        \"content\": \"监控内容:$Message\"
     }
  }" || true
rm -f  /tmp/${name}_${dt}.txt